﻿// Class downloaded from the web to run the google translate api.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Client;
using System.Web;
using System.Net;
using System.Text.RegularExpressions;





namespace OCRGUI2008
{
    public class googleTranslate
    {
        public googleTranslate() { }

        public string TranslateGoogle(string text, string fromCulture, string toCulture)
        {
            fromCulture = fromCulture.ToLower();
            toCulture = toCulture.ToLower();

            // normalize the culture in case something like en-us was passed 
            // retrieve only en since Google doesn't support sub-locales
            string[] tokens = fromCulture.Split('-');
            if (tokens.Length > 1)
                fromCulture = tokens[0];

            // normalize ToCulture
            tokens = toCulture.Split('-');
            if (tokens.Length > 1)
                toCulture = tokens[0];

            //string url = string.Format(@"http://translate.google.com/translate_a/t?client=j&text={0}&hl=en&sl={1}&tl={2}",
              //   HttpUtility.UrlEncode(text), fromCulture, toCulture);
            string url = string.Format(@"https://www.googleapis.com/language/translate/v2?key=AIzaSyCwMNswAxDCBS1u75leTOZ89OcduTeiZ6k&source={1}&target={2}&q={0}",
                                       HttpUtility.UrlEncode(text), fromCulture, toCulture);

            // Retrieve Translation with HTTP GET call
            string html = null;
            try
            {
                System.Net.WebClient web = new System.Net.WebClient();

                // MUST add a known browser user agent or else response encoding doesn't return UTF-8 (WTF Google?)
                web.Headers.Add(System.Net.HttpRequestHeader.UserAgent, "Mozilla/5.0");
                web.Headers.Add(System.Net.HttpRequestHeader.AcceptCharset, "UTF-8");

                // Make sure we have response encoding to UTF-8
                web.Encoding = Encoding.UTF8;
                html = web.DownloadString(url);
            }
            catch (Exception ex)
            {
                string ErrorMessage = "";
                return ErrorMessage;
            }

            // Extract out trans":"...[Extracted]...","from the JSON string
           // string result = Regex.Match(html, "trans\":(\".*?\"),\"", RegexOptions.IgnoreCase).Groups[1].Value;
            string result = Regex.Match(html,"\"translatedText\": (\".*?\")", RegexOptions.IgnoreCase).Groups[1].Value;
           // html = "{\n \"data\": {\n  \"translations\": [\n   {\n    \"translatedText\": \"Hallo\"\n   }\n  ]\n }\n}\n"
            if (string.IsNullOrEmpty(result))
            {
                string ErrorMessage = "";
                return ErrorMessage;
            }

            //return WebUtils.DecodeJsString(result);

            // Result is a JavaScript string so we need to deserialize it properly
            //JavaScriptSerializer ser = new JavaScriptSerializer();
            //return ser.Deserialize(result, typeof(string)) as string;

            return result;
        }
    }
}
